<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-12-02
 * Time: 8:43
 */

namespace App\Services\TagService;


use App\Models\Position;
use App\Models\Tag;
use App\Models\Tagged;

class TagService
{
    public function add($tagged, $tag)
    {
        if ($tag instanceof Tag) {
            $tag = $tag->id;
        }
        /** @var Position $tagged */
        $count = $tagged->tags()->where('tag_id', $tag)->count();
        if ($count <= 0) {
            $tagged->tags()->save(new Tagged(['tag_id' => $tag]));
        }
        return $this;
    }

    public function remove($tagged, $tag)
    {
        if ($tag instanceof Tag) {
            $tag = $tag->id;
        }
        /** @var Position $tagged */
        $tagged->tags()->where('tag_id', $tag)->delete();
        return $this;
    }

    public function clean($tagged)
    {
        /** @var Position $tagged */
        $tagged->tags()->delete();
    }

    public function toggle($tagged, $tag)
    {
        if ($tag instanceof Tag) {
            $tag = $tag->id;
        }
        /** @var Position $tagged */
        $count = $tagged->tags()->where('tag_id', $tag)->count();
        if ($count <= 0) {
            $tagged->tags()->save(new Tagged(['tag_id' => $tag]));
        } else {
            $tagged->tags()->where('tag_id', $tag)->delete();
        }
        return $this;
    }

    public function position(Position $position)
    {
        $position->tags()->where('is_robot', true)->delete();
        $tag  = Tag::whereName('职位')->first();
        $tags = Tag::whereDescendantOf($tag)->whereIsLeaf()->get();
        foreach ($tags as $tag) {
            /** @var Tag $tag */
            if ($tag->condition) {
                $result = true;
                foreach ($tag->condition as $key => $value) {
                    switch ($value[0]) {
                        case ">":
                            $result = $result && ($position->$key > $value[1]);
                            break;
                        case ">=":
                            $result = $result && ($position->$key >= $value[1]);
                            break;
                        case "=":
                            $result = $result && ($position->$key == $value[1]);
                            break;
                        case "!=":
                            $result = $result && ($position->$key != $value[1]);
                            break;
                        case "<":
                            $result = $result && ($position->$key < $value[1]);
                            break;
                        case "<=":
                            $result = $result && ($position->$key <= $value[1]);
                            break;
                        case "include":
                            if(is_array($value[1])){
                                $temp_result=false;
                                foreach ($value[1] as $v){
                                    $temp_result=$temp_result||str_contains($position->$key, $v);
                                }
                                $result = $result && $temp_result;
                            }else{
                                $result = $result && (str_contains($position->$key, $value[1]));
                            }
                            break;
                        default:
                            $result = false;
                    }
                }
            } else {
                $result = false;
            }
            if ($result) {
                $position->tags()->save(new Tagged(['tag_id' => $tag->id, 'is_robot' => true]));
            }
        }
    }
}